import json
from abaqus import *
from abaqusConstants import *
from driverUtils import *

executeOnCaeStartup()

# Open output database
odb = session.openOdb(r'{odb_path}')

# get nodes from odb
key = odb.rootAssembly.instances.keys()[0]
nodes = odb.rootAssembly.instances[key].nodes
elements = odb.rootAssembly.instances[key].elements

# get displacement for all nodes
results = {{"nodes":[], "elements":[]}}
for node in nodes:
    label = node.label
    u = odb.steps['Step-1'].frames[-1].fieldOutputs['U'].values[label-1]
    data = u.data
    results["nodes"].append({{"nodeLabel": label, "ux": float(
        data[0]), "uy": float(data[1]), "uz": float(data[2])}})
for element in elements:
    label = element.label
    s = odb.steps['Step-1'].frames[-1].fieldOutputs['S'].values[label-1]
    results["elements"].append({{"elementLabel": label, "s1": s.maxPrincipal, "s2": s.midPrincipal, "s3":s.minPrincipal}})


with open(r"{output_path}", "w") as file:
    json.dump(results, file)